package verify

import (
	"context"
	dnsapi "dns-tools/pkg/dns"
)

// DoHDNSResolver 适配 pkg/dns 的 Resolver 为 BatchVerifier 需要的 DNSResolver
type DoHDNSResolver struct {
	r dnsapi.Resolver
}

func NewDoHDNSResolver(r dnsapi.Resolver) *DoHDNSResolver {
	if r == nil {
		return nil
	}
	return &DoHDNSResolver{r: r}
}

// LookupTXT 通过 DoH API 查询 TXT 记录
func (d *DoHDNSResolver) LookupTXT(ctx context.Context, domain string) ([]string, error) {
	recs, err := d.r.Query(ctx, domain, dnsapi.TypeTXT)
	if err != nil {
		return nil, err
	}
	out := make([]string, 0, len(recs))
	for _, rr := range recs {
		out = append(out, rr.Value)
	}
	return out, nil
}

// LookupCNAME 通过 DoH API 查询 CNAME 记录（返回第一个值）
func (d *DoHDNSResolver) LookupCNAME(ctx context.Context, domain string) (string, error) {
	recs, err := d.r.Query(ctx, domain, dnsapi.TypeCNAME)
	if err != nil {
		return "", err
	}
	if len(recs) == 0 {
		return "", nil
	}
	return recs[0].Value, nil
}
